package com.fuqi.android.zhongcai.base.view.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.mvp.presenter.LoginPresenterImpl;
import com.fuqi.android.zhongcai.base.mvp.presenter.LoginPresenterInter;
import com.fuqi.android.zhongcai.base.mvp.view.ILoginView;
import com.fuqi.android.zhongcai.base.view.widget.CustomProgress;
import com.fuqi.android.zhongcai.business.shuig.OrderDict;
import com.fuqi.android.zhongcai.sql.User;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.gson.GsonHelper;

import butterknife.Bind;
import butterknife.OnClick;


public class LoginActivity extends BaseActivity implements ILoginView {

    @Bind(R.id.login_register)
    TextView login_register;
    @Bind(R.id.login_forgetpass)
    TextView login_forgetpass;
    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_password)
    EditText et_password;


    private LoginPresenterInter loginPresenter;


    @OnClick({R.id.btn_login, R.id.login_register, R.id.login_forgetpass})
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_login:
                CustomProgress.show(LoginActivity.this,"正在登录...",true,null);
                loginPresenter.login(et_username.getText().toString().trim(), et_password.getText().toString().trim());
                break;
            case R.id.login_forgetpass:
                ActivityUtils.startActivity(LoginActivity.this, ForgetPassFirstActivity.class);
                break;
            case R.id.login_register:
                ActivityUtils.startActivity(LoginActivity.this, RegisterActivity.class);
                break;
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginPresenter = new LoginPresenterImpl(this);
        configStausBar();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CustomProgress.close();
    }


    protected void configStausBar() {

        if (hasKitKat() && !hasLollipop()) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else if (hasLollipop()) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#00ffffff"));
        }

    }


    @Override
    public void loginSuccess(User user) {

        CustomProgress.close();
        ActivityUtils.toast("登录成功");
        UniApplication.getInstance().deleteUserInfo();
        if(OrderDict.RoleType.TYPE_GLY.equals(user.getRolecode()))
            user.setRolecode(OrderDict.RoleType.TYPE_NQ);
        UniApplication.getInstance().setUserInfo(user);
        ActivityUtils.startActivityNeedLogin(LoginActivity.this,MainTabActivity.class);
        Log.i("juner", "juner login " + GsonHelper.writeValue(user));
        finish();

    }

    @Override
    public void loginFailed() {
        CustomProgress.close();
        ActivityUtils.toast("登录失败");
    }

    @Override
    public void loginException() {
        CustomProgress.close();
    }

    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public boolean hasTitleBar() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

}
